package com.lzh.others;


import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

/**
 * 全局时间工具类
 *
 * @author lin
 */
public class DateUtil {

    /**
     * 兼容不规范的iso8601时间格式
     */
    private static final String ISO8601_COMPATIBLE_PATTERN = "yyyy-M-d'T'H:m:s";

    /**
     * 用以输出标准的iso8601时间格式
     */
    private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * wvp内部统一时间格式
     */
    public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String zoneStr = "Asia/Shanghai";

    public static final DateTimeFormatter formatterCompatibleISO8601 = DateTimeFormatter.ofPattern(ISO8601_COMPATIBLE_PATTERN, Locale.getDefault()).withZone(ZoneId.of(zoneStr));
    public static final DateTimeFormatter formatterISO8601 = DateTimeFormatter.ofPattern(ISO8601_PATTERN, Locale.getDefault()).withZone(ZoneId.of(zoneStr));
    public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN, Locale.getDefault()).withZone(ZoneId.of(zoneStr));

    public static String yyyy_MM_dd_HH_mm_ssToISO8601(String formatTime) {

        return formatterISO8601.format(formatter.parse(formatTime));
    }

    public static String ISO8601Toyyyy_MM_dd_HH_mm_ss(String formatTime) {
        return formatter.format(formatterCompatibleISO8601.parse(formatTime));

    }

    /**
     * yyyy_MM_dd_HH_mm_ss 转时间戳
     *
     * @param formatTime
     * @return
     */
    public static long yyyy_MM_dd_HH_mm_ssToTimestamp(String formatTime) {
        TemporalAccessor temporalAccessor = formatter.parse(formatTime);
        Instant instant = Instant.from(temporalAccessor);
        return instant.getEpochSecond();
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNow() {
        LocalDateTime nowDateTime = LocalDateTime.now();
        return formatter.format(nowDateTime);
    }

    /**
     * 格式校验
     *
     * @param timeStr           时间字符串
     * @param dateTimeFormatter 待校验的格式
     * @return
     */
    public static boolean verification(String timeStr, DateTimeFormatter dateTimeFormatter) {
        try {
            LocalDate.parse(timeStr, dateTimeFormatter);
            return true;
        } catch (DateTimeParseException exception) {
            return false;
        }
    }

    public static String getNowForISO8601() {
        LocalDateTime nowDateTime = LocalDateTime.now();
        return formatterISO8601.format(nowDateTime);
    }

    /**
     * 秒转换小时:分钟:秒
     *
     * @param timestamp 秒为单位
     * @return 比如...02:03:52
     */
    public static String secToTime(long timestamp) {
        long hour = timestamp / 3600;
        long minute = (timestamp - hour * 3600) / 60;
        long second = (timestamp - hour * 3600 - minute * 60);

        StringBuffer sb = new StringBuffer();
        if (hour > 0) {
            sb.append(":");
            if (second < 10) {
                sb.append("0").append(hour);
            } else {
                sb.append(hour);
            }
        } else {
            sb.append("00");
        }
        if (minute > 0) {
            sb.append(":");
            if (second < 10) {
                sb.append("0").append(minute);
            } else {
                sb.append(minute);
            }
        } else {
            sb.append(":").append("00");
        }
        if (second > 0) {
            sb.append(":");
            if (second < 10) {
                sb.append("0").append(second);
            } else {
                sb.append(second);
            }
        } else {
            sb.append(":").append("00");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String timeStr = "2019-1-1T1:1:1";
        String timeStr2 = "2019-1-1T1:2:2";
        String s = DateUtil.ISO8601Toyyyy_MM_dd_HH_mm_ss(timeStr);
        String s2 = DateUtil.ISO8601Toyyyy_MM_dd_HH_mm_ss(timeStr2);
        System.out.println("s = " + s);
        System.out.println("s2 = " + s2);
        long l = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(s);
        System.out.println("l = " + l);
        long l2 = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(s2);
        System.out.println("l2 = " + l2);
        long timestamp = l2 - l;
        String s1 = secToTime(timestamp);
        System.out.println("s1 = " + s1);
//        LocalTime localTime = parse.toLocalTime();
//        String s1 = localTime.toString();
//        System.out.println("s1 = " + s1);
    }
}
